//
//  BIZRemoteNotificationWebVC.m
//  biz
//
//  Created by zhangfuwei on 2017/4/22.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZRemoteNotificationWebVC.h"
#import <WebKit/WebKit.h>

@interface BIZRemoteNotificationWebVC ()<UIWebViewDelegate>
/** webView */
@property (nonatomic, strong)  UIWebView    *webView;
/** imgID */
@property (nonatomic, copy) NSString *imgId;

@end

@implementation BIZRemoteNotificationWebVC


- (instancetype)initWithWebContentId:(NSString *)imgId {
    self = [super init];
    if (self) {
        _imgId = imgId;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.webView];
    if (_imgId) {
        [self requestContentWithImgId:_imgId];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -- 代理数据源
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [MBProgressHUD showActivityMessageInView:nil];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [MBProgressHUD showTipMessageInView:@"请检查网络"];

}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [MBProgressHUD hideHUD];
}

- (void)requestContentWithImgId:(NSString *)imgId {
    [RequestTool POST:kCycleScrollViewImgDetailURL
           parameters:@{@"uuid":imgId}
             progress:nil
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  NSString *code = responseObject[@"code"];
                  NSString *data = responseObject[@"data"];
                  if (code.intValue == 1) {
                      
                      NSString *HTML = [data stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
                      [self.webView loadHTMLString:HTML baseURL:nil];
                  }
              } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  [MBProgressHUD showErrorMessage:error.description];
              }];
}
#pragma mark -- 私有方法
-(UIWebView *)webView {
    if (!_webView) {
        CGRect frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-64);
        _webView = [[UIWebView alloc] initWithFrame:frame];
        _webView.delegate = self;
        
    }
    return _webView;
}

@end
